import Vue from 'vue'
import Index from './index.vue'

let MessageConstructor = Vue.extend(Index)
let instance = null
let instances = []
let sn = 1

const Message = function (options) {
  if (typeof options === 'string') {
    options = { message: options }
  }

  instance = new MessageConstructor({
    data: options,
  })
  instance.id = 'message' + sn++
  options.onClose = function () {
    Message.close(instance.id)
  }
  instance.$mount()
  instance.visible = true
  document.body.appendChild(instance.$el)
  instances.push(instance)
  return instance
}

Message.close = function (id) {
  for (let i = 0; i < instances.length; i++) {
    let item = instances[i]
    if (item.id === id) {
      instances.splice(i, 1)
      break
    }
  }
}

export default Message
